using System;
using System.Collections.Generic;
using System.Text;
using FriendsNearby.Services.Entities;
using Facebook.Service.Core;
using Microsoft.Practices.EnterpriseLibrary.PolicyInjection;
using System.Linq.Expressions;

namespace FriendsNearby.Services
{

    public class UsersService : IUsersService
    {
        DataAccess dataAccess;
        //ILocationService locationService;

        public UsersService(/*ILocationService locationService, */DataAccess dataAccess)
        {
            this.dataAccess = dataAccess;
            //this.locationService = locationService;
        }

        #region IUsersService Members


		//public void UnregisterFacebookUser(string userId)
		//{
		//    dataAccess.RemoveUserPreferences(userId);
		//    locationService.RemoveLocation(userId);
		//    dataAccess.RemoveUserRegistry(userId);
		//}

		public UserRegistry GetUserPreferences(Guid userID)
        {
			UserRegistry user = dataAccess.QueryUserRegistry(x => x.UserID == userID);

			if (user == null) throw new InvalidOperationException(string.Format("User didnt exist in registry userID {0}", userID));
			
			return user;
        }

        public void SetDefaultUserPreferences(Guid userID)
        {
            SetUserPreferences(userID, null, 50, VisibilityLevel.ContactsAndTheirContacts);
        }

		public void SetUserPreferences(Guid userID, string twitterScreenName, int nearbyRadiusKM, VisibilityLevel visibilityLevel)
        {
            dataAccess.SetUserPreferences(userID, twitterScreenName, nearbyRadiusKM, visibilityLevel);
        }

        public IEnumerable<Guid> GetAllUsers()
        {
            return dataAccess.GetAllUsers();
        }

        #endregion

		public UserRegistry QueryUserRegistry(Expression<Func<UserRegistry, bool>> filter)
		{
			return dataAccess.QueryUserRegistry(filter);
		}


		#region IUsersService Members


		public Guid RegisterUser()
		{
			var newUserID = Guid.NewGuid();
            SetDefaultUserPreferences(newUserID);

			return newUserID;
		}

		#endregion
	}
}
